package monitor

import (
	"net/http"

	"github.com/prometheus/client_golang/prometheus"
	"github.com/prometheus/client_golang/prometheus/promhttp"
)

//注册器和监控采集器
var registry *prometheus.Registry
var gatherers prometheus.Gatherers
var defaultGatherers prometheus.Gatherers

//接收http请求，向REST API发送监控响应数据
func SendMetrics(serviceName string, defaultSwitch bool, request *http.Request, response http.ResponseWriter) {
	registryAndGatherMetrics(serviceName, defaultSwitch)
	promHttpHandler := promhttp.HandlerFor(
		gatherers,
		promhttp.HandlerOpts{
			ErrorLog:           *new(Logger),
			ErrorHandling:      promhttp.ContinueOnError,
			DisableCompression: true,
		})

	promHttpHandler.ServeHTTP(response, request)
}

//接收http请求，向REST API发送监控响应数据(prometheus默认指标)
func SendPrometheusMetrics(request *http.Request, response http.ResponseWriter) {
	registryAndGatherPrometheusMetrics()
	promHttpHandler := promhttp.HandlerFor(
		defaultGatherers,
		promhttp.HandlerOpts{
			ErrorLog:           *new(Logger),
			ErrorHandling:      promhttp.ContinueOnError,
			DisableCompression: true,
		})

	promHttpHandler.ServeHTTP(response, request)
}

//ErrorLog Logger
type Logger interface {
	Println(v ...interface{})
}
